package com.hongmeng.teamwork.controller;


import com.hongmeng.teamwork.entity.base.Result;
import com.hongmeng.teamwork.entity.dto.AttachmentDto;
import com.hongmeng.teamwork.factory.attachment.AttachmentFactory;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Log4j2
@RestController
@RequestMapping("/attachment")
public class AttachmentController {

    @Resource
    private AttachmentFactory attachmentFactory;

    @PostMapping("/upload")
    public Result<?> upload(AttachmentDto attachmentDto) {
        try {
            String attachmentKey = attachmentFactory.upload(attachmentDto);
            if (log.isInfoEnabled()) {
                log.info("AttachmentController.upload.attachmentKey: {}", attachmentKey);
            }
            return Result.ok(attachmentKey);
        } catch (Exception e) {
            return Result.fail("附件上传失败！");
        }
    }
}
